(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('alertUserController',alertUserController);
    alertUserController.$inject=['$scope','$filter','toaster','AlertUserService'];
    function alertUserController($scope,$filter, toaster,AlertUser) {
        var notify = function (type, title, message) {
            toaster.pop(type, title, message);
        };

        $scope.condition={
            userId:'',
            userName:''
        };
        $scope._initPage=true;
        $scope.toolbar=[
            "create",
            {
                template: '<kendo-button ng-click="update($event)"><span class="k-icon k-i-edit"></span>编辑</kendo-button>'
            },
            {
                template: '<kendo-button ng-click="destroy($event)"><span class="k-icon k-i-delete"></span>删除</kendo-button>'
            }

        ];
        $scope.pageable={
            refresh: true,
            pageSizes: true,
            numeric: false,
            input:true
        };
        $scope.Model = kendo.data.Model.define({
            id: "userId",
            fields: {
                userId: {type: "string", validation: {required: true}},
                userName: {type: "string",validation: {required: true}},
                userMail: {type:"string",validation: {required: true}},
                userPhone: {type:"string",validation: {required: true}},
                recCreateTime: {type: "string"},
                recCreator: {type:"string"}

            }
        });
        $scope.columns=[
            {
                field: "userId", title: "工号",width:100
            },{
                field: "userName", title: "姓名",width:100
            },{
                field: "userMail", title: "邮箱",width:250
            },{
                field: "userPhone", title: "手机",width:150
            },{
                field: "recCreateTime", title: "操作时间",width:200,
                editor:function(container){
                    $('label[for=recCreateTime]').parent().remove();
                    container.remove();
                }
            },{
                field: "recCreator", title: "操作人",width:80,
                editor:function(container){
                    $('label[for=recCreator]').parent().remove();
                    container.remove();
                }
            }
        ];

        $scope.dataSource = new kendo.data.DataSource({
            autoBind: false,
            transport: {
                read: function (e) {
                    AlertUser.query($scope.condition).then(function (response) {
                        e.success(response);
                    }, function (error) {
                        e.error(error);
                    });
                },
                create: function(e){
                    AlertUser.save(e.data.models[0]).then(function (response) {
                        e.success(response);
                    }, function (error) {
                        e.error(error);
                    });
                },
                update:function(e){
                    AlertUser.save(e.data.models[0]).then(function (response) {
                        e.success(response);
                    }, function (error) {
                        e.error(error);
                    });
                },
                destroy: function(e){
                    AlertUser.destroy(e.data.models[0]).then(function (response) {
                        e.success(response);
                    }, function (error) {
                        e.error(error);
                    });
                },

                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            requestStart:function (e) {
                if($scope._initPage){
                    e.preventDefault();
                }
            },
            batch: true,
            pageSize: 15,
            schema: {
                model: $scope.Model
            }
        });

        $scope.query=function () {
            $scope._initPage=false;
            $scope.grid.dataSource.read();
        };

        $scope.update=function (e) {
            var selected = $scope.grid.select();
            if (selected.length == 0) {
                //alert('No record selected')
                notify('waring', '提示', '请先选择一行！');
            } else {
                $scope.grid.editRow(selected);
            }
        };
        $scope.destroy=function (e) {
            var selected = $scope.grid.select();
            if (selected.length == 0) {
                //alert('No record selected')
                notify('waring', '提示', '请先选择一行！');
            } else {
                $scope.grid.removeRow(selected);
            }
        };
        $scope.gridRowEdit = function (e) {
            var time= $filter('date')(new Date(), 'yyyyMMddHHmmss');
            e.model.set('recCreateTime',time);
            e.model.set('recCreator','admin');
            /*if (!e.model.isNew()) {
            }

            entity.recCreateTime =time;
            entity.recCreator ='admin';*/
        };
    }
})();
